$('document').ready(function () {
    // 加载页面时触发的事件
    $('#login-register').click(function () {
        if (getCookie('code') == 200 || getCookie('code') == 201 || getCookie('code') == 202 || getCookie('code') == 203) {
            window.location.href = "register.html"
        }
    })
    if (getCookie('code') == 200) {
        $.ajax({
            type: 'GET',
            url: 'http://111.229.120.67/stu/find',
            xhrFields: {
                withCredentials: true
            },
            success: function (data) {
                console.log(data)
                if (data.statusCode == 0) {
                    $('#email-input').val(data.student.email)
                    $('#name-input').val(data.student.realname)
                    $('#company-input').val(data.student.unit)
                } else {
                    $('#myModal-public').modal({})
                    $('#myModal-public h4').html('请求超时')
                    $('#myModal-public-body').html('请求超时，请重试')
                }
            }
        })
    } else if (getCookie('code') == 201) {
        $.ajax({
            type: 'GET',
            url: 'http://111.229.120.67/tea/get-info',
            xhrFields: {
                withCredentials: true
            },
            success: function (data) {
                if (data.statusCode == 0) {
                    $('#email-input').val(data.teaid)
                    $('#company-input').val(data.schoolname)
                    $('#personal-company').html(`<p>职称:</p>
                        <input type="text" id="zc-input" class="form-input">`)
                    $('#zc-input').val(data.profession)
                    $('#personal-name').html(`<p>姓名:</p>
                        <input type="text" id="name-input" class="form-input">`)
                    $('#name-input').val(data.realname)
                    $('#personal-name').after(`
                        <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
                            <p>单位:</p>
                            <input type="text" id="schoolname-input" class="form-input" disabled>
                        </div>
                    `)
                    $('#schoolname-input').val(data.schoolname)
                } else {
                    $('#myModal-public').modal({})
                    $('#myModal-public h4').html('请求超时')
                    $('#myModal-public-body').html('请求超时，请重试')
                }
            }
        })
    } else {
        window.location.href = 'login.html'
    }

    if ($(window).width() > 1000) {
        $('#personal-bottomwrap button').css({
            'width': '20%',
        })
        $('#personal-bottomwrap button').eq(2).css({
            'width': '8%',
        })
    } else if ($(window).width() < 776) {
        $('#personal-bottomwrap button').css({
            'width': '30%',
        })
        $('#personal-bottomwrap button').eq(2).css({
            'width': '18%',
        })
    }
    $(window).resize(function () {
        if ($(window).width() > 1000) {
            $('#personal-bottomwrap button').css({
                'width': '20%',
            })
            $('#personal-bottomwrap button').eq(2).css({
                'width': '8%',
            })
        } else if ($(window).width() < 776) {
            $('#personal-bottomwrap button').css({
                'width': '30%',
            })
            $('#personal-bottomwrap button').eq(2).css({
                'width': '18%',
            })
        }
    })
    // name-input框事件
    $('#name-input').blur(function () {
        if ($('#name-input').val() == '') {
            $('#name-input').css('border', '2px solid red')
            $('#name-input').prop('placeholder', '姓名不能为空!')
        } else {
            $('#name-input').css('border', '2px solid green')
        }
    })
    $('#name-input').keyup(function () {
        if (($('#name-input').val() !== '')) {
            $('#name-input').css('border', '2px solid green')
        } else {
            $('#name-input').css('border', '2px solid red')
            $('#name-input').prop('placeholder', '姓名不能为空!')
        }

    })
    // password-input框事件
    $('#password-input').blur(function () {
        if ($('#password-input').val() == '') {
            $('#password-input').css({
                'border': '2px solid red',
            })
            $('#password-input').prop('placeholder', '密码不能为空!')
            $('#checkpsw').css('cursor', 'auto')
        } else if ($('#password-input').val().length < 9 && $('#password-input').val().length > 0) {
            $('#password-input').css('border', '2px solid green')
            $('#checkpsw').css('cursor', 'pointer')
        }
    })
    $('#password-input').keyup(function () {
        if ($('#password-input').val().length < 9 && $('#password-input').val().length > 0) {
            $('#checkpsw').css('cursor', 'pointer')
            $('#password-input').css('border', '2px solid green')
        } else {
            $('#checkpsw').css('cursor', 'auto')
            $('#password-input').prop('placeholder', '密码不能为空!')
            $('#password-input').css('border', '2px solid red')
        }

    })
    // 查看密码
    $('#checkpsw').css('cursor', 'pointer')
    $('#checkpsw').click(function () {
        if ($('#password-input').attr('type') == 'password') {
            $('#password-input').attr('type', 'text')
        } else if ($('#password-input').attr('type') == 'text') {
            $('#password-input').attr('type', 'password')
        }
    })

    // email-input框事件
    var emailzz = new RegExp("^[a-z0-9A-Z]+[- | a-z0-9A-Z . _]+@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-z]{2,}$");
    $('#email-input').blur(function () {
        if ($('#email-input').val() == '') {
            $('#email-input').css({
                'border': '2px solid red',
            })
            $('#email-input').prop('placeholder', '邮箱不能为空!')
        } else if (!emailzz.test($('#email-input').val())) {
            $('#email-input').prop('placeholder', '邮箱格式不正确!')
            $('#email-input').val('')
        } else {
            $('#email-input').css('border', '2px solid green')
        }
    })
    $('#email-input').keyup(function () {
        if (emailzz.test($('#email-input').val())) {
            $('#email-input').css('border', '2px solid green')
        } else if (!emailzz.test($('#email-input').val())) {
            $('#email-input').prop('placeholder', '邮箱格式不正确!')
            $('#email-input').css('border', '2px solid red')
        }

    })
    // company-input框事件
    $('#company-input').blur(function () {
        if ($('#company-input').val() == '') {
            $('#company-input').css({
                'border': '2px solid red',
            })
            $('#company-input').prop('placeholder', '工作单位不能为空!')
        } else if ($('#company-input').val().length > 0) {
            $('#company-input').css('border', '2px solid green')
        }
    })
    $('#company-input').keyup(function () {
        if ($('#company-input').val().length > 0) {
            $('#company-input').css('border', '2px solid green')
        } else {
            $('#company-input').prop('placeholder', '工作单位不能为空!')
            $('#company-input').css('border', '2px solid red')
        }
    })
    // zc-input框事件
    $('#RetrievePsw-content').on('mouseover', function () {
        $('#zc-input').blur(function () {
            if ($('#zc-input').val() == '') {
                $('#zc-input').css({
                    'border': '2px solid red',
                })
                $('#zc-input').prop('placeholder', '职称不能为空!')
            } else if ($('#zc-input').val().length > 0) {
                $('#zc-input').css('border', '2px solid green')
            }
        })
        $('#zc-input').keyup(function () {
            if ($('#zc-input').val().length > 0) {
                $('#zc-input').css('border', '2px solid green')
            } else {
                $('#zc-input').prop('placeholder', '职称不能为空!')
                $('#zc-input').css('border', '2px solid red')
            }
        })
    })

    // -------------------------------------------------------------
    // 修改信息
    $('#update').click(function () {
        if(getCookie('code')==200){
            $.ajax({
                type: 'POST',
                url: 'http://111.229.120.67/stu/update',
                data:{
                    "unit":$('#company-input').val(),
                    "realname":$('#name-input').val()
                },
                xhrFields: {
                    withCredentials: true
                },
                success: function (data) {
                    if (data.statusCode == 0) {
                        $('#myModal-public').modal({})
                        $('#myModal-public h4').html('修改信息成功')
                        $('#myModal-public-body').html('修改成功！')
                        setTimeout(function(){
                            window.location.href="personal.html"
                        },1000)
                    } else {
                        $('#myModal-public').modal({})
                        $('#myModal-public h4').html('修改信息失败')
                        $('#myModal-public-body').html('修改失败，请重试')
                    }
                }
            })
        }
        if(getCookie('code')==201){
            $.ajax({
                type: 'POST',
                url: 'http://111.229.120.67/tea/update-tea',
                data:{
                    "profession":$('#zc-input').val(),
                    "realname":$('#name-input').val()
                },
                xhrFields: {
                    withCredentials: true
                },
                success: function (data) {
                    if (data.statusCode == 0) {
                        $('#myModal-public').modal({})
                        $('#myModal-public h4').html('修改信息成功')
                        $('#myModal-public-body').html('修改成功！')
                        setTimeout(function(){
                            window.location.href="personal.html"
                        },1000)
                    } else {
                        $('#myModal-public').modal({})
                        $('#myModal-public h4').html('修改信息失败')
                        $('#myModal-public-body').html('修改失败，请重试')
                    }
                }
            })
        }
    })
    // 注销
    $('#cancel').click(function () {
        $.ajax({
            type: 'GET',
            url: 'http://111.229.120.67/gen/logout',
            xhrFields: {
                withCredentials: true
            },
            success: function (data) {
                if (data.statusCode == 0) {
                    clearCookie('code')
                    window.location.href = 'login.html'
                } else {
                    clearCookie('code')
                    window.location.href = 'login.html'
                }
            }
        })
    })
    // 修改密码
    $('#personal-updatepsw').click(function () {
        $('#myModal-public h4').html('修改密码')
        $('#myModal-public-body').html(`
            旧的密码：<input type="password" id="personal-newpsw" placeholder="不输入则保留原密码">
            <br>
            <br>
            新的密码：<input type="password" id="personal-newpsw1" placeholder="请输入新密码">
            <br>
            <br>
            确认密码：<input type="password" id="personal-newpsw2" placeholder="请再次输入新密码">
            `)
        $('#myModal-public .modal-footer').html(`
            <button type="button" class="btn btn-primary" id="personal-upade-pswsure">确认修改</button>
            <button type="button" class="btn" data-dismiss="modal">关闭</button>
            `)
        $('#myModal-public').modal({

        })
    })
    $('#myModal-public').on('mouseover', function () {
        // 确认修改密码
        $('#personal-upade-pswsure').off("click").click(function () {
            if ($('#personal-newpsw2').val() !== $('#personal-newpsw1').val()) {
                $('#check-repeat').remove()
                $('#personal-newpsw2').after(`<p id="check-repeat" style="color:red">两次输入的密码不相同，请重新输入</p>`)
            } else {
                $.ajax({
                    type: 'POST',
                    url: 'http://111.229.120.67/gen/update-pasw',
                    data:{
                        "oldpassword":$('#personal-newpsw').val(),
                        "newpassword":$('#personal-newpsw1').val()
                    },
                    xhrFields: {
                        withCredentials: true
                    },
                    success: function (data) {
                        if (data.statusCode == 0) {
                            $('#myModal-public-body').html('密码修改成功')
                            $('#myModal-public .modal-footer').html(`
                            <button type="button" class="btn" data-dismiss="modal">关闭</button>
                            `)
                        }else if(data.statusCode == -1){
                            $('#myModal-public h4').html('修改失败')
                            $('#myModal-public-body').html('修改密码失败，请重试')
                            $('#myModal-public .modal-footer').html(`
                            <button type="button" class="btn" data-dismiss="modal">关闭</button>
                            `)
                        }else {
                            $('#myModal-public h4').html('请求超时')
                            $('#myModal-public-body').html('请求超时，请重试')
                            $('#myModal-public .modal-footer').html(`
                            <button type="button" class="btn" data-dismiss="modal">关闭</button>
                            `)
                        }
                    }
                })
            }
        })
    })

    // --------------------------
})